// pages/order-refund/index.js
import { getUserOrderDetail,joinRefund } from '../../api/order'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: {
      skuPrice: 0
    },
    join: [],
    refundRemark: ''
  },
  /**
   * 生命周期函数--监听页面显示
   */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.orderid){
      this.setData({ orderid: options.orderid}) 
    }
    
    var pages = getCurrentPages(); //页面指针数组
    var prepage = pages[pages.length - 2]; //上一页面指针
    if (prepage.route == "pages/order/index") { //订单列表页进入
      this._getUserOrderDetail()
    } else { // 订单详情页进入
      if(options.all=='1'){
        this._getUserOrderDetail()
      }else{
        this.selectUser(prepage.data)
      }
    }
  },
  onShow() {
  },
  //全选
  selectUserALL(data){
    let num = 0
    data.join.forEach((e, i) => {
      if (e.refundStatus == 0) {
        num++
        e.select = true
      }
    })
    if (num == 0) {
      wx.navigateBack()
    } else {
      this.setData({ ...data })
    }
  },
  //处理
  selectUser(data) {
    let num = 0
    data.join.forEach((e, i) => {
      e.select = i == data.bindIndex
      if (e.refundStatus == 0) {
        num++
      }
    })
    if (num == 0) {
      wx.navigateBack()
    } else {
      this.setData({ ...data })
    }
  },
  //获取详情
  _getUserOrderDetail() {
    getUserOrderDetail(this.data.orderid).then(res => {
      let obj = res.data;
      let { actualPrice, totalPrice, integralPrice, couponPrice } = obj.order
      let price = Number(totalPrice) - Number(actualPrice)
      obj.order.price = price > 0 ? price.toFixed(2) : price
      obj.order.totalPrice = totalPrice.toFixed(2)
      obj.order.actualPrice = (actualPrice.toFixed(2) + '').split('.')
      obj.order.integralPrice = integralPrice > 0 ? integralPrice.toFixed(2) : integralPrice
      obj.order.couponPrice = couponPrice > 0 ? couponPrice.toFixed(2) : couponPrice
      this.selectUserALL(obj)
    })
  },
  getValue(e) {
    this.setData({
      refundRemark: app.removeBlank(e.detail.value)
    })
  },
  choosestate(e) {
    let {index,state} = e.currentTarget.dataset
    if (state == 0) {
      this.setData({
        ['join[' + index + '].select']: !this.data.join[index].select
      })
    }
  },
  _joinRefund(e) {

    let {refundRemark,join,order} = this.data
    let {orderId} = this.data.order
    let str = []
    let price = 0
    join.forEach(e => {
      if (e.select) {
        str.push(e.joinId)
            if(this.data.order.skuType==1){
            price += order.skuPrice
            }else{
              price=this.data.order.totalPrice
            }
      }
    })
    if (str.length>0 && refundRemark != '') {
      wx.showModal({
        title: '提示',
        content: '确认要取消吗',
        showCancel: true, //是否显示取消按钮
        cancelText: "返回", //默认是“取消”
        cancelColor: '#999999', //取消文字的颜色
        confirmText: "确定", //默认是“确定”
        confirmColor: '#666666', //确定文字的颜色
        success:res=>{
          if (!res.cancel) {
            joinRefund({orderId: orderId,refundRemark: refundRemark,writePrice: price,joinIds: str}).then(res => {
              if (res.code == 0) {
                wx.showToast({title: '审核中'})
                join.forEach(e => {
                  if (e.select) {
                    e.select=false
                    e.refundStatus = 1
                  }
                })
                this.setData({join: join})
                wx.navigateBack()
              } else {
                wx.showToast({title: res.msg,icon: 'none'})
              }
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: str.length<1 ? '请选择退款人员' : '请填写退款原因',
        icon: 'none'
      })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})